package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.util.AliyunOSSUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategroyServiceImpl implements CategroyService{
    @Autowired
    private DtsCategoryMapper categoryMapper;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Override
    public IPage<DtsCategory> queryCategroy(Integer page, Integer limit, String sort, String order) {
        IPage<DtsCategory> iPage=new Page<>(page,limit);
        QueryWrapper<DtsCategory> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }
        IPage<DtsCategory> categoryIPage = categoryMapper.selectPage(iPage, queryWrapper);
        return categoryIPage;
    }

    @Override
    public List<CatVo> queryCategory() {
        List<CatVo> catVos = categoryMapper.queryCategory();
        return catVos;
    }

    @Override
    public DtsCategory add(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        category.setDeleted(false);
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public int updateCategroy(DtsCategory category) {
        category.setUpdateTime(new Date());
        int updateById = categoryMapper.updateById(category);
        return updateById;
    }

    @Override
    public void deleteCateGroy(DtsCategory category) {
        String iconUrl = category.getIconUrl();
        int IconindexOf = iconUrl.lastIndexOf("/");
        int IconindexOf1 = iconUrl.lastIndexOf("?");
        String iconUuid = iconUrl.substring(IconindexOf + 1, IconindexOf1);

        String picUrl = category.getPicUrl();
        int picIndex = picUrl.lastIndexOf("/");
        int picIndexOf = picUrl.lastIndexOf("?");
        String picUuid = picUrl.substring(picIndex + 1, picIndexOf);
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,iconUuid);
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,picUuid);

        Integer id = category.getId();
        QueryWrapper<DtsCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);

    }
}
